package com.news.web.servlet.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.entity.PageBean;
import com.news.entity.Review;
import com.news.service.NewsService;
import com.news.service.ReviewService;
import com.news.service.impl.NewsServiceImpl;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class NewsBlogServlet
 */
@WebServlet("/NewsBlogServlet")
public class NewsBlogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 创建map
		Map<String, Object> blogMap = new HashMap<String, Object>();
		// 创建数组
		String[] strType = { "移动设备", "照相机", "设计", "平板电脑", "产品", "其它" };
		// 随机数生成
		Random random = new Random();
		int randomNumber = random.nextInt(6); // 生成0到5之间的随机数（包括0和5）
		// 处理页数
		String result = request.getParameter("currentPageNum");
		int currentPageNum = Integer.parseInt(result == null ? "1" : result);
		String pageSizeResult = request.getParameter("pageSize");
		int pageSize = Integer.parseInt(result == null ? "4" : pageSizeResult);
		// 调用分页查询方法,传入查询类型。查询页数，查询条数
		PageBean<News> date = newsService.pageList(strType[randomNumber], currentPageNum, pageSize);
		// 数据存入map
		blogMap.put("date", date);
		// 查询右侧栏热度最高的四条新闻
		List<News> queryRightNewsHeatNumList = newsService.queryNewsHeatNum(4);
		// 装入数据
		blogMap.put("queryRightNewsHeatNumList", queryRightNewsHeatNumList);
		// 查询右侧栏评论最高的四条新闻
		List<News> queryNewsReviewNum = newsService.queryNewsReviewNum(4);
		// 装入数据
		blogMap.put("queryNewsReviewNum", queryNewsReviewNum);
		// 创建右侧Reviewlist
		List<Review> ReviewRightList = reviewService.likeSelectListReviewNum(4);
		// 创建右侧Newslist
		List<News> NewsRightList = new ArrayList<News>();
		// 循环获取对应的News
		for (Review review : ReviewRightList) {
			News newsRight = reviewService.selectNews(review.getNewsId());
			NewsRightList.add(newsRight);
		}
		// 装入数据
		blogMap.put("ReviewRightList", ReviewRightList);
		blogMap.put("NewsRightList", NewsRightList);
		// 将数据存入
		request.setAttribute("blogMap", blogMap);
		// 跳转回原页面
		request.getRequestDispatcher("/News/blog.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
